package com.crane.framework.service.impl;

import com.crane.framework.service.IBpmnService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@Service
@Slf4j
public class IBpmnServiceImpl implements IBpmnService {

  @Resource private RuntimeService runtimeService;
  @Resource private RepositoryService repositoryService;

  @Override
  public void createDeploy(
      String resourceName, String name, org.springframework.core.io.Resource resource)
      throws IOException {
    Deployment deployment =
        repositoryService
            .createDeployment()
            .addInputStream(resourceName, resource.getInputStream())
            .name(name)
            .deploy();
    log.info("流程部署id: {}", deployment.getId());
    log.info("流程部署名称: {}", deployment.getName());
  }

  @Override
  public ProcessInstance startProcessInstanceAssignVariables(
      String processDefinitionId, String businessKey, Map<String, Object> variables) {
    ProcessInstance processInstance =
        runtimeService.startProcessInstanceById(processDefinitionId, businessKey, variables);
    log.info("流程定义ID: {}", processInstance.getProcessDefinitionId());
    log.info("流程实例ID: {}", processInstance.getId());
    log.info("BusinessKey: {}", processInstance.getBusinessKey());
    return processInstance;
  }
}
